package com.promotores;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GestorTipoEvento extends CreaTablas{
	CreaTablas db;	

	// TipoEvento table name
	private static final String TABLE_TIPOEVENTO = "TipoEvento";

	// TipoEvento Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_TIPO_EVENTO = "tipoEvento";
	private static final String KEY_FLAG_SAVED = "flagSaved";
	private static final String KEY_FLAG_DELETED = "deleted";
    private static final String KEY_CREATED_AT = "created_at";
	private static final String KEY_UPDATED_AT = "updated_at";
	
	public GestorTipoEvento(Context context) {
		super(context);
		db = new CreaTablas(context);
	}
    
	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new tipoEvento
	public void addTipoEvento(BeanTipoEvento tipoEvento) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_ID, tipoEvento.getId());
		values.put(KEY_TIPO_EVENTO, tipoEvento.getTipoEvento()); // Contact Name
		values.put(KEY_FLAG_SAVED, tipoEvento.getSaved());
		values.put(KEY_FLAG_DELETED, tipoEvento.getDeleted());
		values.put(KEY_CREATED_AT, tipoEvento.getCreated_at());
		values.put(KEY_UPDATED_AT, tipoEvento.getUpdated_at());		

		// Inserting Row
		db.insert(TABLE_TIPOEVENTO, null, values);
		db.close(); // Closing database connection
	}

	// Getting single tipoEvento
	public BeanTipoEvento getTipoEvento(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		String selectQuery = "SELECT  * FROM " + TABLE_TIPOEVENTO + " WHERE ID = " + id + ";";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null)
			cursor.moveToFirst();
		BeanTipoEvento tipoEvento = new BeanTipoEvento();
		tipoEvento.setId(Integer.parseInt(cursor.getString(0)));
		tipoEvento.setTipoEvento(cursor.getString(1));
		tipoEvento.setSaved(cursor.getString(2));
		tipoEvento.setDeleted(cursor.getString(3));
		tipoEvento.setCreated_at(cursor.getString(4));
		tipoEvento.setUpdated_at(cursor.getString(5));
		// return contact
		cursor.close();
		db.close();
		return tipoEvento;
	}
	
	// Getting All tipoEvento
	public List<BeanTipoEvento> getAllTipoEvento() {
		List<BeanTipoEvento> listaTipoEventos = new ArrayList<BeanTipoEvento>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_TIPOEVENTO;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				BeanTipoEvento tipoEvento = new BeanTipoEvento();
				tipoEvento.setId(Integer.parseInt(cursor.getString(0)));
				tipoEvento.setTipoEvento(cursor.getString(1));
				tipoEvento.setSaved(cursor.getString(2));
				tipoEvento.setDeleted(cursor.getString(3));
				tipoEvento.setCreated_at(cursor.getString(4));
				tipoEvento.setUpdated_at(cursor.getString(5));
				// Adding tipoEvento to list
				listaTipoEventos.add(tipoEvento);
			} while (cursor.moveToNext());
		}
		cursor.close();
		db.close();
		return listaTipoEventos;
	}

	// Updating single tipoEvento
	public void updateTipoEvento(BeanTipoEvento tipoEvento) {
		SQLiteDatabase db = this.getWritableDatabase();
		String consulta = "UPDATE " + TABLE_TIPOEVENTO 
						  + " SET " + KEY_UPDATED_AT + " = "
						  + tipoEvento.getUpdated_at()
						  + ", " + KEY_FLAG_DELETED + " = "
						  + tipoEvento.getDeleted()
						  + ", " + KEY_TIPO_EVENTO + " = "
						  + tipoEvento.getTipoEvento()
						  + " WHERE " + KEY_ID + " = " + tipoEvento.getId() + ";";
		db.execSQL(consulta);
		db.close();
	}

	// Deleting single tipoEvento
	public void deleteTipoEvento(BeanTipoEvento tipoEvento) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_TIPOEVENTO, KEY_ID + " = ?",
				new String[] { String.valueOf(tipoEvento.getId()) });
		db.close();
	}

	public void deleteAll() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DELETE FROM " + TABLE_TIPOEVENTO);	
		db.close();
	}

	// Getting tipoEvento Count
	public int getTipoEventoCount() {
		String countQuery = "SELECT  * FROM " + TABLE_TIPOEVENTO;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		db.close();
		return cursor.getCount();
	}
}